<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\system\sms;

use think\App;
use app\controller\property\Base;
use app\common\services\CacheService;
use app\validate\system\sms\SystemSmsConfigValidate;
use app\common\repositories\system\sms\SystemSmsConfigRepository;


class SmsConfig extends Base
{

    protected $repository;

    public function __construct(App $app, SystemSmsConfigRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }

    /**
     *
     * @return string|\think\response\Json|\think\response\View
     * @throws \Exception
     *
     * @author Amos
     * 2024-01-22 17:12
     */
    public function configUpdate()
    {
        $info = $this->repository->getConfig($this->request->propertyId);
        if (!$info) {
            $param = $this->request->param([
                'signature' => '',
            ]);
            $info = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'signature' => '',
                'sms_platform' => '',
                'verify_img_code' => '',
                'send_sms_time_out' => '',
                'auth_sms_time_out' => '',
                'aliyun_access_key_id' => '',
                'aliyun_access_key_secret' => '',
                'juhe_key' => '',
                'webchinese_sms_account' => '',
                'webchinese_sms_key' => '',
                'tencent_appid' => '',
                'tencent_secret_id' => '',
                'tencent_secret_key' => '',
                'zhutong_username' => '',
                'zhutong_password' => '',
            ]);
            validate(SystemSmsConfigValidate::class)->scene('edit')->check($param);
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->propertyUId);
                if ($res) {
                    CacheService::create($this->request->propertyId)->delete('property_sms_config_' . $this->request->propertyId);
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑文件参数失败', $e);
                return $this->error('网络错误');
            }
        } else {
            return $this->fetch('system/sms/config/update_info', [
                'info' => $info,
            ]);
        }
    }

}